﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Text.RegularExpressions;

namespace OlapProject
{
    class PrianPageParser
    {
        HtmlDocument propertyPage;
        PropertyVO data = new PropertyVO();
        
        Dictionary<string, string> typeMap = new Dictionary<string,string>();

        
        public PrianPageParser (string link, HtmlWeb http) 
        {
            propertyPage = http.Load(link);
           
            typeMap["penthouse"] = "penthouse";
            typeMap["flat"] = "flat";
            typeMap["apartment"] = "apartment";
            typeMap["bungalow"] = "bungalow";
            typeMap["villa"] = "villa";
            typeMap["house"] = "house";
            typeMap["cottage"] = "cottage";
            typeMap["townhouse"] = "townhouse";
            typeMap["mansion"] = "mansion";
            typeMap["studio"] = "studio";
            typeMap["chalet"] = "detached";
            typeMap["unspecified"] = "unspecified";
        }
        private void addType()
        {
            string title = propertyPage.DocumentNode.SelectSingleNode("//div[@class=\"desc\"]//h1").InnerText;

            int pos = title.IndexOf(' ');

            string type = title.Substring(0, pos);

            type = Regex.Replace(type, "[^a-zA-Z]", "");

            data.type = type.ToLower();

            if (typeMap.ContainsKey(data.type)) {
                data.type = typeMap[data.type];
            } else {
                data.type = typeMap["unspecified"];
            }
            
        }
        private void addAddress()
        {
            //adres
            string addressLabel = "";

            if (propertyPage.DocumentNode.SelectSingleNode("//table[@class=\"cont\"]//strong") != null)
            {
                addressLabel = propertyPage.DocumentNode.SelectSingleNode("//table[@class=\"cont\"]//strong").InnerText;
            }
            string country, city, district;
            //czasem moze nie byc
            if (addressLabel == "Address:")
            {
                string address = propertyPage.DocumentNode.SelectSingleNode("//table[@class=\"cont\"]//td[2]").InnerText;

                address = Regex.Replace(address, "[\\n\\t]", "");
                string[] addressArray;

                addressArray = address.Split(',');

                country = addressArray[0];
                city = addressArray.Length > 1 ? addressArray[1] : "";
                district = addressArray.Length > 2 ? addressArray[2] : "";

                data.country = country.ToLower().Trim();
                data.city = city.ToLower().Trim();
                data.district = district.ToLower().Trim();
            }
        }
        private void addOtherData()
        {
            //podstawowe informacje
            HtmlNodeCollection basicInfo = propertyPage.DocumentNode.SelectNodes("//p[@class=\"long\"]");

            if (basicInfo == null) return;

            foreach (HtmlNode p in basicInfo)
            {
                string key = p.ChildNodes[0].InnerText;
                //ucinam dwukropek na koncu
                key = key.Substring(0, key.Length - 1);

                string value = p.ChildNodes[1].InnerText;

                //cena w euro
                if (key == "Sale price")
                {
                    //mozna usunac znaczki euro
                    value = Regex.Replace(value, "\\s|&#[\\d]+;", "");
                    data.price = decimal.Parse(value);
                }
                else if (key == "Total Rooms"){
                    //data.totalRooms = int.Parse(value);
                }
                else if (key == "Number of bedrooms"){
                    data.bedrooms = int.Parse(value);
                }
                else if (key == "Number of bathrooms"){
                    data.bathrooms = int.Parse(value);
                }
                //m^2
                else if (key == "House Area" || key == "Floor Area")
                {
                    value = Regex.Replace(value, "[^\\d]+", "");
                    data.area = int.Parse(value);
                }
                //m^2 dzialka
                else if (key == "Plot Area")
                {
                    value = Regex.Replace(value, "[^\\d]+", "");
                    data.plotArea = int.Parse(value);
                }
                //pietro
                else if (key == "Storey")
                {
                    //data.storey = int.Parse(value);
                }
                //numer pietra
                else if (key == "Number of storeys")
                {
                    //data.storeys = int.Parse(value);
                }

                data.source = "prian";
                data.datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }
        public PropertyVO getData () 
        {
            addAddress();
            addOtherData();
            addType();

            return data;
        }    
    }
}
